package OddMuse;

=head1 NAME

OddMuse

=head1 DESCRIPTION

This module includes a variety of functions which are indended to co-exist
with the functions provided by the OddMuse script itself.  It's also used to
set default variables, and to include other modules which provide functions
in the default OddMuse namespace as well.

=head1 GLOBAL VARIABLES

=over 4

=item * $SearchBanned 

A list of pages, seperated by a "|" which are to be excluded from search
results.  This affects not only regular searches, but other search-related
functions such as the display of journal or collections.

=item * $UseTweakedFooterTimestamp

Use the more tweaked footer timestamp provided by this module, or use the
OddMuse Default.  The tweaked footer is much more verbose, and might not
be for everyone.

=back

=cut

our $SearchBanned              = "WikiTag|HomePage";
our $UseTweakedFooterTimestamp = "1";

=head1 INCLUDED MODULES

The following modules exist in the OddMuse namespace only!  The methods they
call often exist in proper namespaces, but it made sense to break up related
OddMuse hackery into seperate files based on function.  I'll figure out a
better system later.

=cut

$VERSION   = ( q$Revision: 42 $ ) =~ /(\d+)/g;
use OddMuse::Searching::Engine;
use OddMuse::Searching::EvilDoers;

use OddMuse::Database::SmartTitles;
use OddMuse::Database::Building;
use OddMuse::Database::Tagging;
use OddMuse::Database::Backlinks;
use OddMuse::Database::PageData;

use strict;

=head1 FUNCTIONS

=over 4

=item * unCamel

This function takes a CamelCase name as input, and (hopefully) returns that
same string in a more Human Readable format.  Eg:

 in  = CharlesPeterMauch
 out = Charles Peter Mauch

=cut

sub unCamel {
    my $result;
    my @namearray = split( /([A-Z]+)/, $_[0] );

    if ( $_[0] =~ /^\d\d\d\d-\d\d-\d\d.*/ ) {
        $_[0] =~ s/\_/\ /g;
      return $_[0];
    }

    foreach my $val ( @namearray ) {
        if ( $val =~ /[A-Z]/ ) {
            $result .= " $val";
        } else {
            $result .= "$val";
        }
    }
    $result =~ s/.//s;
    $result =~ s/\_/\ /g;
    $result =~ s/\ \ /\ /g;
  return $result;
} ## end sub unCamel

=item * GetParentLink

Tries to determine if a valid WikiName contains a "parent" link.  For example, this
would return CharlesMauch, if given CharlesMauchResume if CharlesMauch actually
exists.  Buggy and unused at the moment.

=cut

sub GetParentLink {
    my ( $result, $first, $numchunks, $stem, $popped, @names );
    my @namebits = split( /([A-Z]+)/, $_[0] );

    foreach my $chunk ( @namebits ) {
        $chunk =~ s/\.//s;      # Remove .'s from chunk
        $chunk =~ s/\_/\ /g;    # Replace _'s with spaces
        if ( $chunk =~ /[A-Z]/ ) {
            $first = $chunk;
        } else {
            push( @names, $first . $chunk );
            $first = '';
        }
    } ## end foreach my $chunk ( @namebits)
    shift( @names );    # Remove blank from array, not sure why it exists
    $popped    = pop( @names );    # Drop last item in array
    $numchunks = @names;
    while ( $numchunks > 1 ) {
        $stem = join( '', @names );    # Merge array back into string
        $result .=
          ", which is a part of <a href=\"$stem\" title=\"Parent link is $stem\">"
          . unCamel( $stem ) . "</a>";
        pop( @names );
        $numchunks = @names;
    } ## end while ( $numchunks > 1 )
  return $result
      . ", which is a part of <a href=\"/HomePage\" title=\"Go Home!\">this site</a>.</p>";
} ## end sub GetParentLink

=item * CustomGetFooterTimestamp

Displays a very verbose set of information about the current page versions,
when it was created, etc.  Only called if user sets $UseTweakedFooterTimestamp.

Makes extensive use of OddMuse::Date.

=cut

if ( $UseTweakedFooterTimestamp ) {
    *OldGetFooterTimestamp = *GetFooterTimestamp;
    *GetFooterTimestamp    = *CustomGetFooterTimestamp;
}

sub CustomGetFooterTimestamp {
    my ( $id, $rev ) = @_;
    my $result;
    if ( $Page{ created } ) {
        my @creation = (
                        "Version <strong>1</strong> was created on <em>"
                      . &OddMuse::Date::CustomCalcDay( $Page{ created } ) . " ("
                      . &OddMuse::Date::LongTimeSince( $Page{ created } )
                      . " ago)</em> by "
                      . $q->a( { -href  => $Page{ originalAuthor },
                                 -title => 'Page Creator: '
                                   . unCamel( $Page{ originalAuthor } )
                               },
                               $Page{ originalAuthor }
                             )
                       );
        $result .= $q->p( @creation );
    } ## end if ( $Page{ created } )
    if ( $id and $rev ne 'history' and $rev ne 'edit' and $Page{ revision } ) {
        if ( $Page{ revision } > '1' ) {
            my @elements;
            push( @elements,
                  'Revison <strong>' . $Page{ revision } . '</strong> was' );
            if ( $UseDiff ) {
                push( @elements,
                      ScriptLinkDiff( 2, $id, T( 'last edited' ), $rev ) );
            } else {
                push( @elements, 'last edited' );
            }
            push( @elements, ' on<em>' );
            push( @elements, &OddMuse::Date::CustomCalcDay( $Page{ ts } ) );
            push( @elements,
                  '(' . &OddMuse::Date::LongTimeSince( $Page{ ts } ) );
            push( @elements, ' ago)' );
            push( @elements, '</em>by ' );
            push( @elements,
                  GetAuthorLink( $Page{ host }, $Page{ username } ) );
            $result .= $q->p( @elements );

        } ## end if ( $Page{ revision }...
    } ## end if ( $id and $rev ne 'history'...
  return $result;
} ## end sub CustomGetFooterTimestamp

1;

__END__

=back

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to C<cmauch@gmail.com>

=head1 AUTHOR

Charles Mauch <cmauch@gmail.com>

=head1 LICENSE

Copyright (c) 2006 Charles Mauch

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA  02110-1301, USA.

=head1 SEE ALSO

perl(1).

=cut

# $Id: OddMuse.pm 42 2006-10-01 05:02:06Z cmauch $
